﻿Imports Sistema_InformacionNaviera.BC
Imports Sistema_InformacionNaviera.BE
Imports Sistema_InformacionNavier.UTIL


Public Class GestionarTarifa

    'Inherits System.Web.UI.Page
    Inherits PaginaBase

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If Not Page.IsPostBack Then


            CargarPais()
            CargarPuerto("PT")
            CargarServicio("SR")
            CargarPuertoOperaciones("PT")
            sTab.ActiveTabIndex = 0
            If Session("TipoTransaccionTarifaFlete") = 0 Then  ' Nuevo 
                lblTitulo.Text = "Nueva Tarifa Flete"
                CrearTablaTemporalTarifa()
                txtCodCons.Enabled = True
                txtCodEmb.Enabled = True
            ElseIf Session("TipoTransaccionTarifaFlete") = 1 Then  ' Modifica 
                lblTitulo.Text = "Modificar Tarifa Flete"
                CrearTablaTemporalTarifa()
                CargarDatosTarifaFlete()
                txtCodCons.Enabled = False
                txtCodEmb.Enabled = False
            End If

        End If


        If My.Request.Params("__EVENTTARGET") = "MiFuncionTarifa" Then
            If Session("BPCodTarifa") = Nothing Then
                txtTarifaFlete.Text = String.Empty
            Else
                'txtTarifaFlete.Text = Session("BPCodTarifa")
                txtTarifaFlete.Text = Session("BPDesTarifa")

            End If
        End If



        If My.Request.Params("__EVENTTARGET") = "MiFuncionPaquetes" Then

            If Session("BSSCodigoEntidadEMB") = Nothing Then
                txtCodEmb.Text = String.Empty
            Else
                txtCodEmb.Text = Session("BSSCodigoEntidadEMB")
                txtDescEmb.Text = Session("BSSNombreEntidadEMB")

            End If


            If Session("BSSCodigoEntidadCON") = Nothing Then
                txtCodCons.Text = String.Empty
            Else
                txtCodCons.Text = Session("BSSCodigoEntidadCON")
                txtDesCons.Text = Session("BSSNombreEntidadCON")

            End If


        End If


    End Sub

    Public Sub CargarPais()

        Dim listPaisBE As List(Of UbigeoBE) = New List(Of UbigeoBE)
        Dim objPaisBC As UbigeoBC = New UbigeoBC()

        Dim objPaisSeleccione As UbigeoBE = New UbigeoBE
        objPaisSeleccione.CodPai = 0
        objPaisSeleccione.DesPai = "[Seleccione]"
        listPaisBE.Add(objPaisSeleccione)

        listPaisBE.AddRange(objPaisBC.ListaPaís())

        cboPais.DataSource = listPaisBE
        cboPais.DataValueField = "CodPai"
        cboPais.DataTextField = "DesPai"
        cboPais.DataBind()
    End Sub

    Public Sub CargarPuerto(ByVal sTipo As String)


        Dim listGeneralBE As List(Of GeneralBE) = New List(Of GeneralBE)
        Dim objGeneralBC As GeneralBC = New GeneralBC()

        Dim objUnidadSeleccione As GeneralBE = New GeneralBE
        objUnidadSeleccione.Codigo = 0
        objUnidadSeleccione.Descripcion = "[Seleccione]"
        listGeneralBE.Add(objUnidadSeleccione)
        objUnidadSeleccione.Codigo = sTipo

        listGeneralBE.AddRange(objGeneralBC.ListadoGeneral(objUnidadSeleccione))

        cboPuerto.DataSource = listGeneralBE
        cboPuerto.DataValueField = "Codigo"
        cboPuerto.DataTextField = "Descripcion"
        cboPuerto.DataBind()

    End Sub

    Public Sub CargarPuertoOperaciones(ByVal sTipo As String)


        Dim listGeneralBE As List(Of GeneralBE) = New List(Of GeneralBE)
        Dim objGeneralBC As GeneralBC = New GeneralBC()

        Dim objUnidadSeleccione As GeneralBE = New GeneralBE
        objUnidadSeleccione.Codigo = 0
        objUnidadSeleccione.Descripcion = "[Seleccione]"
        listGeneralBE.Add(objUnidadSeleccione)
        objUnidadSeleccione.Codigo = sTipo

        listGeneralBE.AddRange(objGeneralBC.ListadoGeneral(objUnidadSeleccione))

        cboPuertoOpera.DataSource = listGeneralBE
        cboPuertoOpera.DataValueField = "Codigo"
        cboPuertoOpera.DataTextField = "Descripcion"
        cboPuertoOpera.DataBind()

    End Sub

    Public Sub CargarServicio(ByVal sTipo As String)


        Dim listGeneralBE As List(Of GeneralBE) = New List(Of GeneralBE)
        Dim objGeneralBC As GeneralBC = New GeneralBC()

        Dim objUnidadSeleccione As GeneralBE = New GeneralBE
        objUnidadSeleccione.Codigo = 0
        objUnidadSeleccione.Descripcion = "[Seleccione]"
        listGeneralBE.Add(objUnidadSeleccione)
        objUnidadSeleccione.Codigo = sTipo

        listGeneralBE.AddRange(objGeneralBC.ListadoGeneral(objUnidadSeleccione))

        cboServicio.DataSource = listGeneralBE
        cboServicio.DataValueField = "Codigo"
        cboServicio.DataTextField = "Descripcion"
        cboServicio.DataBind()

    End Sub



    Public Sub CargarDatosTarifaFlete()

        Try
            Dim objTarifaFleteBE As TarifaFleteBE
            Dim objTarifa As List(Of TarifaFleteBE)

            Dim objTarifaFleteBC As TarifaFleteBC
            Dim bExito As Boolean = False
            Dim sMensaje As String = String.Empty
            Dim dtDetalle As New DataTable
            Dim dt As DataTable


            objTarifaFleteBE = New TarifaFleteBE
            objTarifaFleteBE.Tfl_Csne = Session("ClienteTarifaFlete") 'cliente seleccionado

            objTarifaFleteBC = New TarifaFleteBC
            objTarifa = objTarifaFleteBC.ObtieneTarifaFlete(objTarifaFleteBE, bExito, sMensaje, dtDetalle)

            dgBlTarifas.DataSource = dtDetalle
            dgBlTarifas.DataBind()

            If bExito Then

                cboTipo.SelectedValue = objTarifa.Item(0).Tfl_Tipo
                cboTipoMovimiento.SelectedValue = objTarifa.Item(0).Tfl_Tmov
                cboTipoPago.SelectedValue = objTarifa.Item(0).Tfl_Tpag
                txtFecInicio.Text = Convert.ToDateTime(objTarifa.Item(0).Tfl_Fivig).ToString("dd/MM/yyyy") 'objTarifa.Item(0).Tfl_Fivig
                txtFecFin.Text = Convert.ToDateTime(objTarifa.Item(0).Tfl_Ffinvig).ToString("dd/MM/yyyy") 'objTarifa.Item(0).Tfl_Ffinvig
                txtCodCons.Text = objTarifa.Item(0).Tfl_Csne
                txtCodEmb.Text = objTarifa.Item(0).Tfl_Emba
                txtCodBro.Text = objTarifa.Item(0).Tfl_brocker
                txtCodOperLog.Text = objTarifa.Item(0).Tfl_OpeLog
                txtDatosProd.Text = objTarifa.Item(0).Tfl_datosProd
                cboModalidad.SelectedValue = objTarifa.Item(0).Tfl_ModTrans
                cboServicio.SelectedValue = objTarifa.Item(0).Tfl_Servicio
                cboPuertoOpera.SelectedValue = objTarifa.Item(0).Tfl_PtoOper
                txtDesCons.Text = objTarifa.Item(0).Tfl_DescCons
                txtDescEmb.Text = objTarifa.Item(0).Tfl_DescEmb

                Dim i As Integer

                If dtDetalle.Rows.Count > 0 Then

                    dt = Session("tbTempTarifa")

                    Do While i < dtDetalle.Rows.Count

                        Dim dRow As DataRow = dt.NewRow

                        dRow("Tfl_Codi") = dtDetalle.Rows(i).Item(0).ToString
                        dRow("Tfl_Tari") = dtDetalle.Rows(i).Item(11).ToString
                        dRow("Tfl_Umed") = dtDetalle.Rows(i).Item(12).ToString
                        dRow("Tfl_CodPais") = dtDetalle.Rows(i).Item(13).ToString
                        dRow("Tfl_CodPuer") = dtDetalle.Rows(i).Item(14).ToString
                        dRow("Tfl_Mone") = dtDetalle.Rows(i).Item(15).ToString
                        dRow("Tfl_Mont") = dtDetalle.Rows(i).Item(16).ToString
                        dRow("Tfl_Obse") = dtDetalle.Rows(i).Item(21).ToString
                        dRow("Tfl_Est") = dtDetalle.Rows(i).Item(17).ToString

                        dRow("Tfl_Pais") = dtDetalle.Rows(i).Item(26).ToString
                        dRow("Tfl_Puer") = dtDetalle.Rows(i).Item(27).ToString

                        dt.Rows.Add(dRow)
                        Session("tbTempTarifa") = dt
                        i = i + 1
                    Loop

                End If

            End If

            txtCodCons.Enabled = True
            txtCodEmb.Enabled = True



        Catch ex As Exception
            ex.Message.ToString()
        End Try

    End Sub


    'Private Sub AgregarDetalleTarifa(ByVal codPaisdTarifaID As String, ByVal Tfl_Tari As String, ByVal Tfl_Umed As String, ByVal Tfl_Pais As String, ByVal Tfl_Puer As String, ByVal Tfl_Mone As String, ByVal Tfl_Mont As String, ByVal Tfl_Obse As String, ByVal dTarifaEstado As String)
    '    Dim dt As New DataTable
    '    Dim drItem As DataRow
    '    Dim i As Integer = 0
    '    Dim cDetalle As Integer
    '    dt = Session("tbTempTarifa")

    '    If dt.Rows.Count > 0 Then

    '        Dim dRow As DataRow = dt.NewRow
    '        dRow("C_COD_PAIS") = codPais
    '        dRow("T_DES_PAIS") = desPais
    '        dRow("C_COD_CIUDAD") = codCiudad
    '        dRow("T_DES_CIUDAD") = desCiudad
    '        dRow("C_COD_SERVICIO") = codServicio
    '        dRow("T_DES_SERVICIO") = desServicio
    '        dt.Rows.Add(dRow)

    '        Session("tbTempTarifa") = dt
    '        Me.dgDetalle.DataSource = dt
    '        Me.dgDetalle.DataBind()
    '        lblMensaje.Text = ""

    '    End If

    'End Sub

    Public Sub CrearTablaTemporalTarifa()

        Dim dt As New DataTable
        dt.Columns.Add("Tfl_Codi")
        dt.Columns.Add("Tfl_Tari")
        dt.Columns.Add("Tfl_Umed")
        dt.Columns.Add("Tfl_CodPais")
        dt.Columns.Add("Tfl_Pais")
        dt.Columns.Add("Tfl_Puer")
        dt.Columns.Add("Tfl_CodPuer")
        dt.Columns.Add("Tfl_Mone")
        dt.Columns.Add("Tfl_Mont")
        dt.Columns.Add("Tfl_Obse")
        dt.Columns.Add("Tfl_Est")
        Session("tbTempTarifa") = dt

    End Sub


    Protected Sub btnAgregar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAgregar.Click
        Try

            Dim drItem As DataRow
            Dim i As Integer = 0
            Dim dt As DataTable = Nothing
            Dim iValidacion As Integer = 0

            dt = Session("tbTempTarifa")

            '-------------------------------------------------------------------------------------
            If txtTarifaFlete.Text = String.Empty Then
                MsgBox("Ingrese la tarifa.", True, String.Empty)
                Exit Sub
            ElseIf cboUnidadMedida.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione la unidad de medida.", True, String.Empty)
                Exit Sub
            ElseIf cboPais.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione el pais.", True, String.Empty)
                Exit Sub
            ElseIf cboPuerto.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione el puerto.", True, String.Empty)
                Exit Sub
            ElseIf cboTipoMoneda.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione la moneda.", True, String.Empty)
                Exit Sub
            ElseIf txtMontoUnitario.Text = "" Then
                MsgBox("Ingrese el monto.", True, String.Empty)
                Exit Sub
            ElseIf txtObservaciones.Text = String.Empty Then
                MsgBox("Ingrese el lugar de pago.", True, String.Empty)
                Exit Sub
            End If


            '-------------------------------------------------------------------------------------
            'agrega datos a tabla temporal 
            '------------------------------------------------------------------------
            If dt.Rows.Count > 0 Then

                'Para validar si hay registros repetidos
                Do While i < dt.Rows.Count
                    drItem = dt.Rows(i)

                    If (txtTarifaFlete.Text = drItem.Item(1).ToString) Then
                        If Session("tbTempTarifaFleteTransaccion") = 1 Then ' Modificar
                            If Not dt.Rows(i).Item("tbTempTarifa") = "E" Then
                                iValidacion = iValidacion + 1
                            End If
                        Else
                            iValidacion = iValidacion + 1
                        End If
                    End If
                    i = i + 1
                Loop

                '--------------------------------------------------------------------------------------------------------------

                If Session("ActivarModificacion") = 1 Then
                    ActualizarTarifa()
                    Exit Sub
                End If

                If iValidacion = 0 Then
                    Dim dRow As DataRow = dt.NewRow

                    dRow("Tfl_Codi") = Date.Now.Day & Date.Now.Hour & Date.Now.Minute & Date.Now.Second
                    dRow("Tfl_Tari") = Me.txtTarifaFlete.Text
                    dRow("Tfl_Umed") = Me.cboUnidadMedida.SelectedValue
                    dRow("Tfl_CodPais") = Me.cboPais.SelectedValue
                    dRow("Tfl_Pais") = Me.cboPais.SelectedItem
                    dRow("Tfl_Puer") = Me.cboPuerto.SelectedItem
                    dRow("Tfl_CodPuer") = Me.cboPuerto.SelectedValue
                    dRow("Tfl_Mone") = Me.cboTipoMoneda.SelectedValue
                    dRow("Tfl_Mont") = Me.txtMontoUnitario.Text
                    dRow("Tfl_Obse") = Me.txtObservaciones.Text

                    If Session("tbTempTarifaFleteTransaccion") = 1 Then ' Modificar
                        dRow("Tfl_Est") = "N" 'Registro por modificar
                    Else
                        dRow("Tfl_Est") = "0" 'Registro Nuevo
                    End If

                    dt.Rows.Add(dRow)
                    Session("tbTempTarifa") = dt
                    Me.dgBlTarifas.DataSource = SelectDataTable(dt, "Tfl_Est <> 'E'", "")
                    Me.dgBlTarifas.DataBind()
                    Session("ActivarModificacion") = 0
                    Exit Sub
                Else
                    MsgBox("ERROR: Ya se ingreso la misma tarifa.", True, String.Empty)
                    Exit Sub
                End If

            Else
                Dim dRow As DataRow = dt.NewRow

                  dRow("Tfl_Codi") = Date.Now.Day & Date.Now.Hour & Date.Now.Minute & Date.Now.Second
                dRow("Tfl_Tari") = Me.txtTarifaFlete.Text
                dRow("Tfl_Umed") = Me.cboUnidadMedida.SelectedValue
                dRow("Tfl_CodPais") = Me.cboPais.SelectedValue
                dRow("Tfl_Pais") = Me.cboPais.SelectedItem
                dRow("Tfl_Puer") = Me.cboPuerto.SelectedItem
                dRow("Tfl_CodPuer") = Me.cboPuerto.SelectedValue
                dRow("Tfl_Mone") = Me.cboTipoMoneda.SelectedValue
                dRow("Tfl_Mont") = Me.txtMontoUnitario.Text
                dRow("Tfl_Obse") = Me.txtObservaciones.Text

                If Session("tbTempTarifaFleteTransaccion") = 1 Then ' Modificar
                    dRow("Tfl_Est") = "N" 'Registro por modificar
                Else
                    dRow("Tfl_Est") = "0" 'Registro Nuevo
                End If

                dt.Rows.Add(dRow)
                Session("tbTempTarifa") = dt
                Me.dgBlTarifas.DataSource = SelectDataTable(dt, "Tfl_Est <> 'E'", "")
                Me.dgBlTarifas.DataBind()
                Session("ActivarModificacion") = 0
                Exit Sub

            End If


        Catch ex As Exception
            ex.Message.ToString()
        End Try

    End Sub

    Function SelectDataTable(ByVal dt As DataTable, ByVal filter As String, ByVal sort As String) As DataTable

        Dim rows As DataRow()
        Dim dtNew As DataTable
        dtNew = dt.Clone()
        rows = dt.Select(filter, sort)
        For Each dr As DataRow In rows
            dtNew.ImportRow(dr)
        Next
        Return dtNew

    End Function


    Public Sub ActualizarTarifa()

        Try
            Dim i As Integer = 0
            Dim dt As New DataTable
            dt = Session("tbTempTarifa")
            btnCancelar.Enabled = True




            If dt.Rows.Count > 0 Then

                Do While i < dt.Rows.Count

                    If dt.Rows(i).Item("Tfl_Codi").ToString = CType(txtIDTarifaFlete.Text.Trim, String) Then

                        dt.Rows(i).Item("Tfl_Tari") = Me.txtTarifaFlete.Text
                        dt.Rows(i).Item("Tfl_Umed") = Me.cboUnidadMedida.SelectedValue
                        dt.Rows(i).Item("Tfl_CodPais") = Me.cboPais.SelectedValue
                        dt.Rows(i).Item("Tfl_Pais") = Me.cboPais.SelectedItem
                        dt.Rows(i).Item("Tfl_CodPuer") = Me.cboPuerto.SelectedValue
                        dt.Rows(i).Item("Tfl_Puer") = Me.cboPuerto.SelectedItem
                        dt.Rows(i).Item("Tfl_Mone") = Me.cboTipoMoneda.SelectedValue
                        dt.Rows(i).Item("Tfl_Mont") = Me.txtMontoUnitario.Text
                        dt.Rows(i).Item("Tfl_Obse") = Me.txtObservaciones.Text
                        dt.Rows(i).Item("Tfl_Est") = "U"

                        dgBlTarifas.DataSource = dt
                        dgBlTarifas.DataBind()
                        Session("ActivarModificacion") = 0

                    End If
                    i = i + 1
                Loop
            End If

            ActivarControles(True)
            btnCancelar.Visible = False
            LimpiarDatosFletes()

        Catch ex As Exception
            ex.Message.ToString()
        End Try

    End Sub


    Public Sub ActivarControles(ByVal Tipo As Boolean)
        'cboPais.Enabled = Tipo
        'cboCiudad.Enabled = Tipo
        ''txtCodProveedor.Enabled = Tipo
        ''txtRazonSocial.Enabled = Tipo
        'cboTarifa.Enabled = Tipo
    End Sub

    Public Sub LimpiarDatosFletes()

        Me.txtIDTarifaFlete.Text = String.Empty
        Me.txtTarifaFlete.Text = String.Empty
        Me.txtMontoUnitario.Text = String.Empty
        Me.txtObservaciones.Text = String.Empty

        Me.cboUnidadMedida.SelectedValue = 0
        Me.cboPais.SelectedValue = 0
        Me.cboPuerto.SelectedValue = 0
        Me.cboTipoMoneda.SelectedValue = 0

    End Sub

    Private Sub dgBlTarifas_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles dgBlTarifas.RowCommand
        Dim intCodigo As String = e.CommandArgument

        Select Case e.CommandName

            Case "Eliminar"

                If Session("tbTempTarifaFleteTransaccion") = 1 Then ' Modificar
                    Eliminar_OpcionModificar(intCodigo)
                Else
                    Eliminar_OpcionNuevo(intCodigo)
                End If

            Case "Editar"

                Dim dt As New DataTable
                Dim dtSel As New DataTable
                dt = Session("tbTempTarifa")
                dtSel = SelectDataTable(dt, "Tfl_Codi = " & intCodigo, "Tfl_Codi DESC")
                ActivarControles(False)
                btnCancelar.Visible = True

                Me.txtIDTarifaFlete.Text = dtSel.Rows(0).Item("Tfl_Codi").ToString
                Me.txtTarifaFlete.Text = dtSel.Rows(0).Item("Tfl_Tari").ToString
                Me.cboUnidadMedida.SelectedValue = dtSel.Rows(0).Item("Tfl_Umed").ToString
                Me.cboPais.SelectedValue = dtSel.Rows(0).Item("Tfl_CodPais").ToString
                Me.cboPuerto.SelectedValue = dtSel.Rows(0).Item("Tfl_CodPuer").ToString
                Me.cboTipoMoneda.SelectedValue = dtSel.Rows(0).Item("Tfl_Mone").ToString
                Me.txtMontoUnitario.Text = dtSel.Rows(0).Item("Tfl_Mont").ToString
                Me.txtObservaciones.Text = dtSel.Rows(0).Item("Tfl_Obse").ToString

                Session("ActivarModificacion") = 1


        End Select
    End Sub


    Private Sub Eliminar_OpcionModificar(ByVal intCodigo As String)


        Dim i As Integer = 0
        Dim rwEliminar As String
        Dim dt As New DataTable
        dt = Session("tbTempTarifa")

        If dt.Rows.Count > 0 Then

            Do While i < dt.Rows.Count
                rwEliminar = dt.Rows(i).Item("Tfl_Codi").ToString

                If rwEliminar = CType(intCodigo, String) Then

                    If dt.Rows(i).Item("Tfl_Est") = "N" Then ' si es nuevo se elimina definitivamnete
                        Me.dgBlTarifas.Rows(i).Visible = False
                        dt.Rows.Remove(dt.Rows(i))
                        Me.dgBlTarifas.DataSource = SelectDataTable(dt, "Tfl_Est <> 'E'", "")
                        Me.dgBlTarifas.DataBind()
                    Else
                        dt.Rows(i).Item("Tfl_Est") = "E" 'Eliminacion 
                        Me.dgBlTarifas.Rows(i).Visible = False
                        Me.dgBlTarifas.DataSource = SelectDataTable(dt, "Tfl_Est <> 'E'", "")
                        Me.dgBlTarifas.DataBind()
                    End If

                End If

                i = i + 1
            Loop

        End If

    End Sub

    Private Sub Eliminar_OpcionNuevo(ByVal intCodigo As String)

        Dim i As Integer = 0
        Dim rwEliminar As String
        Dim dt As New DataTable
        dt = Session("tbTempTarifa")

        If dt.Rows.Count > 0 Then

            Do While i < dt.Rows.Count
                rwEliminar = dt.Rows(i).Item("Tfl_Codi").ToString

                If rwEliminar = CType(intCodigo, String) Then
                    dgBlTarifas.Rows(i).Visible = False
                    dt.Rows.Remove(dt.Rows(i))
                End If

                i = i + 1
            Loop

        End If

        dgBlTarifas.DataSource = dt
        dgBlTarifas.DataBind()

    End Sub


    Protected Sub btnGrabar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnGrabar.Click
        Try

            Dim sUsuario As String = Session("idIusuario")
            Dim objTarifaFleteBE As TarifaFleteBE

            If Session("TipoTransaccionTarifaFlete") = 1 Then ' Modificar
                objTarifaFleteBE = New TarifaFleteBE
                objTarifaFleteBE.Tfl_Umod = sUsuario
                Modificar()
            Else
                objTarifaFleteBE = New TarifaFleteBE
                objTarifaFleteBE.Tfl_Ucre = sUsuario
                Nuevo()
            End If

        Catch ex As Exception
            ex.Message.ToString()
        End Try
    End Sub


    Public Sub AgregarNuevo()
        Try
            Dim i As Integer = 0
            Dim dt As New DataTable
            dt = Session("tbTempTarifa")
            btnCancelar.Enabled = True

            If dt.Rows.Count > 0 Then

                Do While i < dt.Rows.Count

                    If dt.Rows(i).Item("Tfl_Est").ToString = "0" Then

                        Nuevo()
                        'dt.Rows(i).Item("Tfl_Tari") = Me.txtTarifaFlete.Text
                        'dt.Rows(i).Item("Tfl_Umed") = Me.cboUnidadMedida.SelectedValue
                        'dt.Rows(i).Item("Tfl_CodPais") = Me.cboPais.SelectedValue
                        'dt.Rows(i).Item("Tfl_Pais") = Me.cboPais.SelectedItem
                        'dt.Rows(i).Item("Tfl_CodPuer") = Me.cboPuerto.SelectedValue
                        'dt.Rows(i).Item("Tfl_Puer") = Me.cboPuerto.SelectedItem
                        'dt.Rows(i).Item("Tfl_Mone") = Me.cboTipoMoneda.SelectedValue
                        'dt.Rows(i).Item("Tfl_Mont") = Me.txtMontoUnitario.Text
                        'dt.Rows(i).Item("Tfl_Obse") = Me.txtObservaciones.Text
                        'dt.Rows(i).Item("Tfl_Est") = "U"

                        'dgBlTarifas.DataSource = dt
                        'dgBlTarifas.DataBind()
                        'Session("ActivarModificacion") = 0

                    End If
                    i = i + 1
                Loop
            End If
        Catch ex As Exception

        End Try
    End Sub

    Public Sub Modificar()

        Try



            If cboTipo.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione el tipo de tarifa.", True, String.Empty)
                Exit Sub
            ElseIf cboTipoMovimiento.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione el tipo de movimiento.", True, String.Empty)
                Exit Sub
            ElseIf cboTipoPago.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione el tipo de pago.", True, String.Empty)
                Exit Sub
            ElseIf txtFecInicio.Text = "" Then
                MsgBox("Ingresar fecha de inicio.", True, String.Empty)
                Exit Sub
            ElseIf txtFecFin.Text = "" Then
                MsgBox("Ingresar fecha fin.", True, String.Empty)
                Exit Sub
            ElseIf txtCodCons.Text = "" Then
                MsgBox("Ingrese código de consignatario.", True, String.Empty)
                Exit Sub
            ElseIf txtCodEmb.Text = String.Empty Then
                MsgBox("Ingrese código de embarcador.", True, String.Empty)
                Exit Sub
            End If

            If txtFecInicio.Text.Length > 0 Or txtFecFin.Text.Length > 0 Then
                If CType(txtFecFin.Text, Date) < CType(txtFecInicio.Text, Date) Then
                    MsgBox("La fecha final no puede ser menor que la inicial.", MsgBoxStyle.Information, Me.Title)
                    'Alerta("La fecha final no puede ser menor que la inicial.", False, String.Empty)
                    Exit Sub
                End If
            End If
            '--------------------------------------------------------------------------------------------------------
            ' Ingresar Flete
            '--------------------------------------------------------------------------------------------------------
            Nuevo()
         

            '-------------------------------------------------------------------------------------------------------------

            Dim objTarifaFleteBE As TarifaFleteBE
            Dim objTarifaFleteBC As TarifaFleteBC
            Dim bExito As Boolean = False
            Dim sMensaje As String = String.Empty
            Dim sUsuario As String = Session("idIusuario")


            Dim sTOPE As String = "M"

            Dim sTfl_Tipo As String = cboTipo.SelectedValue
            Dim sTfl_Tmov As String = cboTipoMovimiento.SelectedValue
            Dim sTfl_Tpag As String = cboTipoPago.SelectedValue
            Dim dtTfl_Fivig As String = txtFecInicio.Text
            Dim dtTfl_Ffinvig As String = txtFecFin.Text
            Dim sTfl_Csne As String = txtCodCons.Text
            Dim sTfl_Emba As String = txtCodEmb.Text
            Dim sTfl_brocker As String = txtCodBro.Text
            Dim sTfl_OpeLog As String = txtCodOperLog.Text
            Dim sTfl_datosProd As String = txtDatosProd.Text
            Dim sTfl_ModTrans As String = cboModalidad.SelectedValue
            Dim sTfl_PtoOpe As String = cboPuertoOpera.SelectedValue
            Dim sTfl_Servicio As String = cboServicio.SelectedValue
            Dim sUsuarioCrea As String = Session("idIusuario")
            Dim sUsuarioMod As String = Session("idIusuario")

            objTarifaFleteBE = New TarifaFleteBE
            objTarifaFleteBE.TOPE = sTOPE
            objTarifaFleteBE.Tfl_Codi = CType(0, Integer)
            objTarifaFleteBE.Tfl_Tipo = sTfl_Tipo
            objTarifaFleteBE.Tfl_Tmov = sTfl_Tmov
            objTarifaFleteBE.Tfl_Tpag = sTfl_Tpag
            objTarifaFleteBE.Tfl_Fivig = dtTfl_Fivig
            objTarifaFleteBE.Tfl_Ffinvig = dtTfl_Ffinvig
            objTarifaFleteBE.Tfl_Csne = sTfl_Csne
            objTarifaFleteBE.Tfl_Emba = sTfl_Emba
            objTarifaFleteBE.Tfl_brocker = sTfl_brocker
            objTarifaFleteBE.Tfl_OpeLog = sTfl_OpeLog
            objTarifaFleteBE.Tfl_datosProd = sTfl_datosProd
            objTarifaFleteBE.Tfl_ModTrans = sTfl_ModTrans
            objTarifaFleteBE.Tfl_PtoOper = sTfl_PtoOpe
            objTarifaFleteBE.Tfl_Servicio = sTfl_Servicio
            objTarifaFleteBE.Tfl_Ucre = sUsuarioCrea
            objTarifaFleteBE.Tfl_Umod = sUsuarioMod
            objTarifaFleteBE.Tfl_Est = "M"

            '---------------------------------------------------------
            ' Detalle
            '---------------------------------------------------------

            Dim dtTarifaFlete As New DataTable
            Dim i As Integer
            Dim drItem As DataRow
            dtTarifaFlete = Session("tbTempTarifa")

            '---------------------------------------------------------

            If dtTarifaFlete.Rows.Count > 0 Then
                Do While i < dtTarifaFlete.Rows.Count
                    drItem = dtTarifaFlete.Rows(i)


                    Dim ITfl_Codi As String = CType(drItem.Item("Tfl_Codi").ToString, String)
                    Dim sTfl_Tari As String = CType(drItem.Item("Tfl_Tari").ToString, String)
                    Dim sTfl_Umed As String = CType(drItem.Item("Tfl_Umed").ToString, String)
                    Dim sTfl_Pais As String = CType(drItem.Item("Tfl_CodPais").ToString, String)
                    Dim sTfl_Puer As String = CType(drItem.Item("Tfl_CodPuer").ToString, String)
                    Dim sTfl_Mone As String = CType(drItem.Item("Tfl_Mone").ToString, String)
                    Dim dTfl_Mont As Double = CType(drItem.Item("Tfl_Mont").ToString, Double)
                    Dim sTfl_Obse As String = CType(drItem.Item("Tfl_Obse").ToString, String)

                    objTarifaFleteBE.Tfl_Codi = CType(ITfl_Codi, Integer)
                    objTarifaFleteBE.Tfl_Tari = sTfl_Tari
                    objTarifaFleteBE.Tfl_Umed = sTfl_Umed
                    objTarifaFleteBE.Tfl_Pais = sTfl_Pais
                    objTarifaFleteBE.Tfl_Puer = sTfl_Puer
                    objTarifaFleteBE.Tfl_Mone = sTfl_Mone
                    objTarifaFleteBE.Tfl_Mont = dTfl_Mont
                    objTarifaFleteBE.Tfl_Obse = sTfl_Obse

                    objTarifaFleteBC = New TarifaFleteBC
                    objTarifaFleteBC.ModificarTarifaFlete(objTarifaFleteBE, bExito, sMensaje)

                    i = i + 1
                Loop

            End If

            If bExito = False Then
                MsgBox("Error: Hubieron problemas al ingresar la tarifa de flete.", True, String.Empty)
                Exit Sub
            Else
                txtCodCons.Enabled = False
                txtCodEmb.Enabled = False
                MsgBox("La Tarifa Flete fue ingresada con exito.", True, String.Empty)
                Session("TipoTransaccionTarifaFlete") = 0
                Response.Redirect("Consultar_Tarifa.aspx")

                Exit Sub
            End If


            '--------------------------------------------------------------------------------------------------------


        Catch ex As Exception
            ex.Message.ToString()
        End Try



    End Sub


    Public Sub Nuevo()

        Try

            If cboTipo.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione el tipo de tarifa.", True, String.Empty)
                Exit Sub
            ElseIf cboTipoMovimiento.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione el tipo de movimiento.", True, String.Empty)
                Exit Sub
            ElseIf cboTipoPago.SelectedItem.Text = "[Seleccione]" Then
                MsgBox("Seleccione el tipo de pago.", True, String.Empty)
                Exit Sub
            ElseIf txtFecInicio.Text = "" Then
                MsgBox("Ingresar fecha de inicio.", True, String.Empty)
                Exit Sub
            ElseIf txtFecFin.Text = "" Then
                MsgBox("Ingresar fecha fin.", True, String.Empty)
                Exit Sub
            ElseIf txtCodCons.Text = "" Then
                MsgBox("Ingrese código de consignatario.", True, String.Empty)
                Exit Sub
            ElseIf txtCodEmb.Text = String.Empty Then
                MsgBox("Ingrese código de embarcador.", True, String.Empty)
                Exit Sub
            End If

            If txtFecInicio.Text.Length > 0 Or txtFecFin.Text.Length > 0 Then
                If CDate(txtFecFin.Text) < CDate(txtFecInicio.Text) Then
                    MsgBox("La fecha final no puede ser menor que la inicial.", MsgBoxStyle.Information, Me.Title)
                    'Alerta("La fecha final no puede ser menor que la inicial.", False, String.Empty)
                    Exit Sub
                End If
            End If

            '--------------------------------------------------------------------------------------------------------
            ' Ingresar Flete
            '--------------------------------------------------------------------------------------------------------

            Dim objTarifaFleteBE As TarifaFleteBE
            Dim objTarifaFleteBC As TarifaFleteBC
            Dim bExito As Boolean = False
            Dim sMensaje As String = String.Empty
            Dim sUsuario As String = Session("idIusuario")


            Dim sTOPE As String = "N"

            Dim sTfl_Tipo As String = cboTipo.SelectedValue
            Dim sTfl_Tmov As String = cboTipoMovimiento.SelectedValue
            Dim sTfl_Tpag As String = cboTipoPago.SelectedValue
            Dim dtTfl_Fivig As String = txtFecInicio.Text
            Dim dtTfl_Ffinvig As String = txtFecFin.Text
            Dim sTfl_Csne As String = txtCodCons.Text
            Dim sTfl_Emba As String = txtCodEmb.Text
            Dim sTfl_brocker As String = txtCodBro.Text
            Dim sTfl_OpeLog As String = txtCodOperLog.Text
            Dim sTfl_datosProd As String = txtDatosProd.Text
            Dim sTfl_ModTrans As String = cboModalidad.SelectedValue
            Dim sTfl_PtoOpe As String = cboPuertoOpera.SelectedValue
            Dim sTfl_Servicio As String = cboServicio.SelectedValue
            Dim sUsuarioCrea As String = Session("idIusuario")
            Dim sUsuarioMod As String = Session("idIusuario")

            objTarifaFleteBE = New TarifaFleteBE
            objTarifaFleteBE.TOPE = sTOPE
            objTarifaFleteBE.Tfl_Codi = CType(0, Integer)
            objTarifaFleteBE.Tfl_Tipo = sTfl_Tipo
            objTarifaFleteBE.Tfl_Tmov = sTfl_Tmov
            objTarifaFleteBE.Tfl_Tpag = sTfl_Tpag
            objTarifaFleteBE.Tfl_Fivig = dtTfl_Fivig
            objTarifaFleteBE.Tfl_Ffinvig = dtTfl_Ffinvig
            objTarifaFleteBE.Tfl_Csne = sTfl_Csne
            objTarifaFleteBE.Tfl_Emba = sTfl_Emba
            objTarifaFleteBE.Tfl_brocker = sTfl_brocker
            objTarifaFleteBE.Tfl_OpeLog = sTfl_OpeLog
            objTarifaFleteBE.Tfl_datosProd = sTfl_datosProd
            objTarifaFleteBE.Tfl_ModTrans = sTfl_ModTrans
            objTarifaFleteBE.Tfl_PtoOper = sTfl_PtoOpe
            objTarifaFleteBE.Tfl_Servicio = sTfl_Servicio
            objTarifaFleteBE.Tfl_Ucre = sUsuarioCrea
            objTarifaFleteBE.Tfl_Umod = sUsuarioMod
            objTarifaFleteBE.Tfl_Est = "N"

            '---------------------------------------------------------
            ' Detalle
            '---------------------------------------------------------

            Dim dtTarifaFlete As New DataTable
            Dim i As Integer
            Dim drItem As DataRow
            dtTarifaFlete = Session("tbTempTarifa")

            '---------------------------------------------------------

            If dtTarifaFlete.Rows.Count > 0 Then
                Do While i < dtTarifaFlete.Rows.Count
                    drItem = dtTarifaFlete.Rows(i)

                    'dsfdsf(Tfl_Codi)

                    Dim sTfl_Tari As String = CType(drItem.Item("Tfl_Tari").ToString, String)
                    Dim sTfl_Umed As String = CType(drItem.Item("Tfl_Umed").ToString, String)
                    Dim sTfl_Pais As String = CType(drItem.Item("Tfl_CodPais").ToString, String)
                    Dim sTfl_Puer As String = CType(drItem.Item("Tfl_CodPuer").ToString, String)
                    Dim sTfl_Mone As String = CType(drItem.Item("Tfl_Mone").ToString, String)
                    Dim dTfl_Mont As Double = CType(drItem.Item("Tfl_Mont").ToString, Double)
                    Dim sTfl_Obse As String = CType(drItem.Item("Tfl_Obse").ToString, String)

                    objTarifaFleteBE.Tfl_Tari = sTfl_Tari
                    objTarifaFleteBE.Tfl_Umed = sTfl_Umed
                    objTarifaFleteBE.Tfl_Pais = sTfl_Pais
                    objTarifaFleteBE.Tfl_Puer = sTfl_Puer
                    objTarifaFleteBE.Tfl_Mone = sTfl_Mone
                    objTarifaFleteBE.Tfl_Mont = dTfl_Mont
                    objTarifaFleteBE.Tfl_Obse = sTfl_Obse

                    objTarifaFleteBC = New TarifaFleteBC

                    'valida si existe el codigo
                    '--------------------------------------------------------
                    Dim dtExiste As DataTable = Nothing
                    objTarifaFleteBE.Tfl_Codi = CType(drItem.Item("Tfl_Codi").ToString, String)
                    objTarifaFleteBC.ValidarTarifaFlete(objTarifaFleteBE, bExito, sMensaje, dtExiste)
                    If dtExiste.Rows.Count = 0 Then
                        objTarifaFleteBC.InsertarTarifaFlete(objTarifaFleteBE, bExito, sMensaje)
                    End If
                    '--------------------------------------------------------------



                    i = i + 1
                Loop

            End If


            If Not Session("TipoTransaccionTarifaFlete") = 1 Then

                If bExito = False Then
                    MsgBox("Error: Hubieron problemas al ingresar la tarifa de flete.", True, String.Empty)
                    Exit Sub
                Else
                    MsgBox("La Tarifa Flete fue ingresada con exito.", True, String.Empty)
                    Response.Redirect("Consultar_Tarifa.aspx")
                    Exit Sub
                End If
            End If




            '--------------------------------------------------------------------------------------------------------


        Catch ex As Exception
            ex.Message.ToString()
        End Try


    End Sub


End Class